<?php
require_once(APPPATH.'models/Basemodel.php');
class Placemodel extends BaseModel
{
    private static $table = "gm_place";
    
    private static $cols = "id,province,name,c_time,u_id";

    function __construct()
    {
        parent::__construct();
    }

    function  countAll(){
        return $this->db->from(self::$table)->count_all_results();
    }

    function listmap(){
        $array=$this->db->get(self::$table)->result_array();
        $map=array();
        foreach ($array as $place) {
            $map[$place['id']]=$place;
        }
        return $map;
    }

    function  getByPlaceName($name){
        $this->db->where("name",$name);
        return $this->db->get(self::$table)->row_array();
    }



    function  getById($id){
        $this->db->where("id", $id);
        return $this->db->get(self::$table)->row_array();
    }

    function  getByProvinceName($province){
        $this->db->where("province", $province);
        return $this->db->get(self::$table)->row_array();
    }

    function insert($data)
    {
        $result=$this->db->insert(self::$table,$data);
        return $result;
    }

    function update($data, $id)
    {
        $this->db->where("id", $id);
        $result=$this->db->update(self::$table,$data);
        return $result;
    }

    function delete($id)
    {
        $this->db->where("id",$id);
        $result=$this->db->delete(self::$table);
        return $result;
    }

    function  lists(){
        $list=$this->db->get(self::$table)->result_array();
        $map=array();
        foreach($list as $l){
            $map[$l['key']]=$l['value'];
        }
        return $map;
    }

    function  listPlaceByCondition($where,$page,$pageSize)
    {
        $start=($page-1)*$pageSize;
        $sql="select top ".$pageSize." * from ".self::$table." where(id not in (select top ".$start." id from ".self::$table." order by id))"." order by id";
        $query=$this->db->query($sql);
        return $query->result_array();
    }


    function countByCondition($where)
    {
        $sql="select count(1) as num from ".self::$table." where 1=1  ".$where;
        $row=$this->db->query($sql)->row_array();
        return $row['num'];
    }
}